/**
 * 
 */
package com.farm.doc.server.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.farm.core.auth.domain.LoginUser;
import com.farm.core.time.TimeTool;
import com.farm.doc.dao.FarmDocShareDaoInter;
import com.farm.doc.domain.FarmDocShare;
import com.farm.doc.server.FarmDocShareInter;
import com.farm.parameter.FarmParameterService;

/**
 * @author yang_
 *
 */
@Service
public class FarmDocShareInterImpl implements FarmDocShareInter {

	@Resource
	private FarmDocShareDaoInter farmDocShareDaoInter;

	private static final String string = "abcdefghijklmnopqrstuvwxyz";
	private static final int pwdLength = 4;;

	private int getRandom(int count) {
		return (int) Math.round(Math.random() * count);
	}

	private String getRandomString(int length) {
		StringBuffer sb = new StringBuffer();
		int len = string.length();
		for (int i = 0; i < length; i++) {
			sb.append(string.charAt(getRandom(len - 1)));
		}
		return sb.toString();
	}

	@Override
	@Transactional
	public FarmDocShare createDocShare(String docid, LoginUser loginUser) {

		FarmDocShare entity = new FarmDocShare();
		entity.setDocid(docid);
		entity.setCuser(loginUser.getId());
		entity.setValidity("0");
		entity.setCusername(loginUser.getName());
		entity.setCtime(TimeTool.getTimeDate12());
		entity.setEtime(TimeTool.getTimeDate12());
		
		String pwd = getRandomString(pwdLength);
		entity.setPwd(pwd);
		return farmDocShareDaoInter.insertEntity(entity);
	}

	@Override
	@Transactional
	public FarmDocShare queryDocShareById(String id) {
		return farmDocShareDaoInter.getEntity(id);
	}

}
